<script setup>
import { reactive, ref } from 'vue';
import { ElMessage } from 'element-plus';

  const loginRef = ref(null)

    const loginForm = reactive({
      username: '',
      password: ''
    })

    const loginFormRules = {
      username: [
        { required: true, message: '用户名不能为空', trigger: 'blur' },
        { min: 5, max: 15, message: '密码长度为5-15', trigger: 'blur' }
      ],
      password: [
        { required: true, message: '密码不能为空', trigger: 'blur' }
      ]
    }

    const onLogin = () => {
      loginRef.value.validate((valid) => {
        if (!valid) return ElMessage.error('用户名或密码格式不正确')
        if (!verify()) return ElMessage.error('用户名或密码错误') 
        ElMessage.success('登录成功')
      })
    }

    const verify = () => {
      return loginForm.username === 'Admin@123' && loginForm.password === '123456'
    }

    const onCancel = () => {
      loginRef.value.resetFields()
    }

</script>
<template>
  <div class="login-box">
    <el-form 
     ref="loginRef"
     :model="loginForm"
     :rules="loginFormRules"
    >
      <el-form-item prop="username">
        <el-input placeholder="请输入用户名"
         v-model="loginForm.username" 
         prefix-icon="User"
        />
      </el-form-item>
      <el-form-item prop="password">
        <el-input placeholder="请输入密码"
         v-model="loginForm.password"
         type="password"
         show-password
         prefix-icon="Lock"
        />
      </el-form-item>
      <el-form-item>
        <el-button type="primary" @click="onLogin">登录</el-button>
        <el-button @click="onCancel">重置</el-button>
      </el-form-item>
    </el-form>
  </div>
</template>

<style lang="scss">
  * {
    margin: 0;
  }

  body {
    background-color: #ddd;
  }

  .login-box {
    display: flex;
    justify-content: center;
    align-items: center;
    height: 100vh;
    .el-form {
      width: 400px;
      background-color: #fff;
      padding: 40px 50px 20px 50px;

      .el-input {
        height: 40px;
      }

      .el-form-item {
        margin: 20px 0;
      }
    }
  }
  
</style>